import { Injectable } from '@nestjs/common';
import * as COS from 'cos-nodejs-sdk-v5';
import { v4 as uuid } from 'uuid';
import * as path from 'path';

@Injectable()
export class UploadService {
  declare cos: COS;
  constructor() {
    this.cos = new COS({
      SecretId: 'AKID793qiYDgBZpz7BiCZk0QjuzxmTzwU2Yx',
      SecretKey: 'qaDk9KLD8wYxPn5EBvXFuTr6CD3kVM4e',
    });
  }
  uploadObject(fileName: string, FileBuffer: Buffer, prefix = 'image') {
    return this.cos.putObject({
      Bucket: 'zrsjt-1330629495', // 填入您自己的存储桶，必须字段
      Region: 'ap-guangzhou', // 存储桶所在地域，例如 ap-beijing，必须字段
      Key: `${prefix}/${uuid()}${path.extname(fileName)}`, // 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段
      Body: FileBuffer, // 必须
    });
  }
  getObject(filePath:string){
    return this.cos.getObject({
      Bucket: 'zrsjt-1330629495', // 填入您自己的存储桶，必须字段
      Region: 'ap-guangzhou', // 存储桶所在地域，例如 ap-beijing，必须字段
      Key: filePath, 
    });
  }
}
